package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;



@Service
public class UsersService {
    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;
    @DubboReference
    private UserApi userApi;
    //根据id查询信息
    public UserInfoVo findUserInfoById(Long userid) {
        UserInfo Info =  userInfoApi.findById(userid);
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(Info,vo);
        if (Info.getAge() != null){
            vo.setAge(Info.getAge().toString());
        }
        return vo;

    }

    public void updateUserInfo(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    public SettingsVo findSettings() {
        SettingsVo vo = new SettingsVo();
        //获取id
        Long userId = UserHolder.getUserId();
        //查询问题
         Question question = questionApi.findQuestion(userId);
        String txt = question == null ? "hello" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //查询开关
         Settings settings = settingsApi.findSettings(userId);
         if (settings != null){
             vo.setPinglunNotification(settings.getPinglunNotification());
             vo.setGonggaoNotification(settings.getGonggaoNotification());
             vo.setLikeNotification(settings.getLikeNotification());
         }else {
             vo.setPinglunNotification(true);
             vo.setGonggaoNotification(true);
             vo.setLikeNotification(true);
         }
         //查询手机号
        String mobile = UserHolder.getUser().getMobile();
         vo.setPhone(mobile);
         vo.setId(userId);
        return  vo;
    }
    //修改问题
    public void saveQuestions(String content) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //查询是否第一次修改问题
        Question question = questionApi.findQuestion(userId);
        if (question == null){
             question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            question.setTxt(content);
            questionApi.update(question);
        }



    }
    //修改通知
    public void updateSetting(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        //获取id
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findSettings(userId);
        if (settings == null){
            settings = new Settings();
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }
    //分页查询黑名单
    public PageResult blacklist(Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();
        //查询
        IPage<UserInfo> ipage = blackListApi.findById(userId,page,pagesize);


        return new PageResult(page,pagesize,ipage.getTotal(),ipage.getRecords());
    }
    //移除黑名单
    public void deleteBlack(Long id) {
        Long userId = UserHolder.getUserId();
        blackListApi.deleteBlack(userId,id);
    }
    //修改手机号
    public void updatePhone(String phone) {
        User user = new User();
        user.setId(UserHolder.getUserId());
        user.setMobile(phone);
        userApi.updatePhone(user);
    }
}
